#include"BlockQueue.hpp"
#include"Task.hpp"
#include<unistd.h>
void* Comsumer(void* arg)
{
    BlockQueue<Task>* bq = (BlockQueue<Task>*)arg;
    while(1)
    {
        Task t;
        bq->pop(&t);
        t.Run();
    }
}
void* Productor(void* arg)
{
    BlockQueue<Task>* bq = (BlockQueue<Task>*)arg;
    while(1)
    {
        int x = rand() % 20;
        int y = rand() % 20;
        const char* opArr  = "+-*/%";
        char op = opArr[rand() % 5];
        std::cout << "生产者派发了一个任务: " << x << op << y << "=?" << std::endl;
        Task t(x,y,op);
        bq->push(t);
        sleep(1);
    }
}
int main()
{
    srand((long long)time(nullptr));
    BlockQueue<Task> *bq = new BlockQueue<Task>();

    pthread_t c,p;
    pthread_t c1,c2,c3,c4;
    pthread_create(&c, nullptr, Comsumer, (void*)bq);
    pthread_create(&c1, nullptr, Comsumer, (void*)bq);
    pthread_create(&c2, nullptr, Comsumer, (void*)bq);
    pthread_create(&c3, nullptr, Comsumer, (void*)bq);
    pthread_create(&c4, nullptr, Comsumer, (void*)bq);
    pthread_create(&p, nullptr, Productor, (void*)bq);

    pthread_join(c, nullptr);
    pthread_join(c1, nullptr);
    pthread_join(c2, nullptr);
    pthread_join(c3, nullptr);
    pthread_join(c4, nullptr);
    pthread_join(p, nullptr);


    return 0;
}
